﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Ue.Infrastructure.Web.UI;
using Ue.Qcdz.Domain;
using Ue.Qcdz.Domain.ResourceManager;

namespace Ue.Qcdz.UI
{
    public class Page : System.Web.UI.Page
    {
        private SiteContext siteContext;

        public Page()
        {
            var siteConfig = Ue.Qcdz.Domain.TheSiteConfig.SiteConfigService.GetSiteConfig(new Guid("38E42B84-11F4-4D4A-B337-52E342CBC3A0"));

            this.Title = siteConfig.SiteName;
            this.MetaDescription = siteConfig.MetaDescription;
            this.MetaKeywords = siteConfig.MetaKeyworld;
        }


        public SiteContext SiteContext
        {
            get
            {
                if (this.siteContext == null)
                    this.siteContext = SiteContext.GetSiteContext(base.Context);

                return siteContext;
            }
        }

        public ResourceGlobals ResourceGlobals
        {
            get { return ResourceGlobals.Instance; }
        }

        public SiteUrls SiteUrls
        {
            get { return SiteUrls.Instance; }
        }

        /// <summary>
        /// 输出ascx文件生成html
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string RenderViewControl(string filePath)
        {
            RenderViewControl<UserControl> viewControl = new RenderViewControl<UserControl>(filePath);

            return viewControl.RenderControl();
        }

        #region 重载

        protected override void Render(HtmlTextWriter writer)
        {
            //Url重写后调整更改Form中的action链接
            AddMetaContentType();
            base.Render(new Ue.Qcdz.Domain.UrlRewriter.RewriteFormHtmlTextWriter(writer));
        }

        #endregion

        /// <summary>
        /// 添加编码格式、MetaDescription、MetaKeyWords。
        /// </summary>
        protected virtual void AddMetaContentType()
        {
            if (this.Header == null)
                return;

            //添加编码格式，因为生成的静态页面如果不添加编码格式可能造成乱码。
            HtmlMeta meta = new HtmlMeta();
            meta.HttpEquiv = "content-type";
            meta.Content = Response.ContentType + "; charset=" + Response.ContentEncoding.HeaderName;
            this.Header.Controls.Add(meta);

            HtmlMeta metaDescription = new HtmlMeta();
            metaDescription.Name = "description";
            metaDescription.Content = this.MetaDescription;
            this.Header.Controls.Add(metaDescription);

            HtmlMeta metaKeyWord = new HtmlMeta();
            metaKeyWord.Name = "keywords";
            metaKeyWord.Content = this.MetaKeywords;
            this.Header.Controls.Add(metaKeyWord);
        }

        public string MetaDescription { get; set; }

        public string MetaKeywords { get; set; }
    }
}
